package com.xywh.emergency_outbound.mvp.ui.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.xywh.emergency_outbound.mvp.ui.db.entity.DaoMaster;
import com.xywh.emergency_outbound.mvp.ui.db.entity.DaoSession;
import com.xywh.emergency_outbound.mvp.ui.db.entity.MaterialLocal;
import com.xywh.emergency_outbound.mvp.ui.db.entity.MaterialLocalDao;
import com.xywh.emergency_outbound.mvp.ui.entity.Tab1;
import com.xywh.emergency_outbound.mvp.ui.entity.Tab3;

import java.util.List;

/**
 * @author 小星 QQ:753940262
 * @class describe
 * @time 2019/11/8 0008 14:13
 */
public class DbController {

    private static DbController mDbController;
    /**
     * Helper
     */
    private DaoMaster.DevOpenHelper mHelper;//获取Helper对象
    /**
     * 数据库
     */
    private SQLiteDatabase db;
    /**
     * DaoMaster
     */
    private DaoMaster mDaoMaster;
    /**
     * DaoSession
     */
    private DaoSession mDaoSession;
    /**
     * 上下文
     */
    private Context context;
    /**
     * dao
     */
    private MaterialLocalDao mMaterialLocalDao;

    /**
     * 初始化
     *
     * @param context
     */
    public DbController(Context context) {
        this.context = context;
        mHelper = new DaoMaster.DevOpenHelper(context, "material.db", null);
        mDaoMaster = new DaoMaster(getWritableDatabase());
        mDaoSession = mDaoMaster.newSession();
        mMaterialLocalDao = mDaoSession.getMaterialLocalDao();
    }

    /**
     * 获取可写数据库
     *
     * @return
     */
    private SQLiteDatabase getWritableDatabase() {
        if (mHelper == null) {
            mHelper = new DaoMaster.DevOpenHelper(context, "material.db", null);
        }
        SQLiteDatabase db = mHelper.getWritableDatabase();
        return db;
    }

    /**
     * 获取单例
     */
    public static DbController getInstance(Context context) {
        if (mDbController == null) {
            synchronized (DbController.class) {
                if (mDbController == null) {
                    mDbController = new DbController(context);
                }
            }
        }
        return mDbController;
    }

    /**
     * 获取可读数据库
     */
    private SQLiteDatabase getReadableDatabase() {
        if (mHelper == null) {
            mHelper = new DaoMaster.DevOpenHelper(context, "material.db", null);
        }
        SQLiteDatabase db = mHelper.getReadableDatabase();
        return db;
    }

    /**
     * 会自动判定是插入还是替换
     *
     * @param materialLocal
     */
    public void insertOrReplace(MaterialLocal materialLocal) {
        mMaterialLocalDao.insertOrReplace(materialLocal);
    }

    /**
     * 插入一条记录，表里面要没有与之相同的记录
     *
     * @param materialLocal
     */
    public long insert(MaterialLocal materialLocal) {
        if (searchByWhere(materialLocal).size() == 0) {
            return mMaterialLocalDao.insert(materialLocal);
        }
        return 0;
    }

    /**
     * 按条件查询数据
     */
    public List<MaterialLocal> searchByWhere(MaterialLocal materialLocal) {
        List<MaterialLocal> personInfors = (List<MaterialLocal>) mMaterialLocalDao.queryBuilder().where(
                MaterialLocalDao.Properties.MaterialName.eq(materialLocal.getMaterialName()),
                MaterialLocalDao.Properties.StoreName.eq(materialLocal.getStoreName()),
                MaterialLocalDao.Properties.StoreId.eq(materialLocal.getStoreId())
        ).list();
        return personInfors;
    }

    /**
     * 更新数据
     *
     * @param dataBean
     */
    public void update(Tab3.DataBean dataBean) {
        MaterialLocal mOldPersonInfor = mMaterialLocalDao.queryBuilder().where(
                MaterialLocalDao.Properties.MaterialName.eq(dataBean.getMaterialName()),
                MaterialLocalDao.Properties.StoreName.eq(dataBean.getStoreName()),
                MaterialLocalDao.Properties.StoreId.eq(dataBean.getStoreId())
        ).build().unique();//拿到之前的记录
        if (mOldPersonInfor != null) {
            mOldPersonInfor.setStoreNum(dataBean.getStoreNum());
            mMaterialLocalDao.update(mOldPersonInfor);
        }
    }

    /**
     * 查询所有数据
     */
    public List<MaterialLocal> searchAll() {
        List<MaterialLocal> materialLocals = mMaterialLocalDao.queryBuilder().list();
        return materialLocals;
    }

    /**
     * 删除数据
     */
    public void delete(Tab1.DataBean dataBean) {
        mMaterialLocalDao.queryBuilder().where(
                MaterialLocalDao.Properties.MaterialName.eq(dataBean.getMaterialName()),
                MaterialLocalDao.Properties.StoreName.eq(dataBean.getStoreName()),
                MaterialLocalDao.Properties.StoreId.eq(dataBean.getStoreId())
        ).buildDelete().executeDeleteWithoutDetachingEntities();
    }

    public void deleteAll() {
        mMaterialLocalDao.deleteAll();
    }
}
